package rabgame

import (
	"gitee.com/rainbowserver_go/config"
	"github.com/Unknwon/goconfig"
	"github.com/labstack/gommon/log"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type RaiseInfo struct {
	mongodb *mgo.Session
	db      *mgo.Database
	Table   *mgo.Collection
}

type Raise struct {
	Userid       int64
	Eos_amount   string
	Token_amount string
	Raise_amount string
	Createtime   int32
}

func (info *RaiseInfo) Init() {
	cfg, _ := goconfig.LoadConfigFile(config.CONFIG_FILE_NAME)
	mongocfg, _ := cfg.GetSection("mongodb")
	url := "mongodb://" + mongocfg["user"] + ":" + mongocfg["pass"] + "@" + mongocfg["host"] + ":" + mongocfg["port"] + "/" + mongocfg["db"]
	mongodb, err := mgo.Dial(url)
	if err != nil {
		log.Fatal(err)
	}
	//defer mongodb.Close()
	mongodb.SetMode(mgo.Monotonic, true)
	info.mongodb = mongodb
	info.db = mongodb.DB(mongocfg["db"])
	info.Table = info.db.C("raise")
}

func (info *RaiseInfo) Insert(userid int64, eos_amount string, token_amount string, raise_amount string, createtime int32) error {
	//{ "userid": 1, "createtime": 1 }
	cargame := Raise{userid, eos_amount, token_amount, raise_amount, createtime}
	return info.Table.Insert(&cargame)
}

func (info *RaiseInfo) Modify(userid int64, eos_amount string, token_amount string, raise_amount string, createtime int32) error {
	selector := bson.M{"userid": userid, "createtime": createtime}
	data := bson.M{"$set": bson.M{"userid": userid, "eos_amount": eos_amount, "token_amount": token_amount, "raise_amount": raise_amount, "createtime": createtime}}
	return info.Table.Update(selector, data)
}
